package common;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-11-14
 * Time: 15:01
 */

// 这个类用于表示测试过程中需要用到的小组件
public class TestUtils {

    public static ChromeDriver driver;

    // 创建驱动对象
    public static ChromeDriver createDriver() {
        // 判断驱动对象是否已经创建
        driver = new ChromeDriver();
        // 创建隐式等待，确保页面渲染出来
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        // 浏览器全屏
        driver.manage().window().maximize();
        return driver;
    }

    // 截图功能
    public static void doScreenShot(WebDriver driver, String caseName) throws IOException {
        // 截图
        File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        // 生成一个动态文件路径(以日期作为文件夹+（用例名+年月日时分秒）作为文件名)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dirName = sdf.format(System.currentTimeMillis());

        sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String fileName = sdf.format(System.currentTimeMillis());

        String name = dirName+"/"+caseName+fileName;
        // 将图片保存到硬盘
        FileUtils.copyFile(file, new File("D:/javaEE_code/java-ee/testforum/src/main/java/screenshot/"+name+".png"));
    }

}
